﻿using Smart.Model;
using System;
using System.Collections;
using UnityEngine;

namespace Smart.Hotfix
{
    public class Entry
    {
        public static Entry Instance;

        GameObject gameObject;
        Transform transform;

        public Entry(GameObject gameObject)
        {
            this.gameObject = gameObject;
            transform = this.gameObject.transform;
            EventSystem = new EventSystem();
            EventSystem.Init(EventSystem.HOTFIX, HotfixManager.Instance.GetHotfixTypes());
            gameObject.name = "游戏热更层";
            ObjectPool = AddViewComponent<ObjectPool,Transform>(transform);
            Scene = AddViewComponent<SceneManager, Transform>(transform);
            TableComponent = Scene.AddComponent<TableComponent>();
            FrameComponent = Scene.AddComponent<FrameComponent>();
            UnityEngine.Object.DontDestroyOnLoad(this.gameObject);
        }

        public void Dispose()
        {
            TableComponent.Dispose();
            TableComponent = null;
            FrameComponent.Dispose();
            FrameComponent = null;
            Scene.Dispose();
            Scene = null;
            ObjectPool.Dispose();
            ObjectPool = null;
            EventSystem.Dispose();
            EventSystem = null;
            transform = null;
            this.gameObject = null;
        }

        public T AddViewComponent<T,A>(A a) where T : ComponentView, new()
        {
            T component = new T();
            EventSystem.AddComponent(component);
            EventSystem.Awake(component,EventSystem, a);
            return component;
        }

        public SceneManager Scene;
        public ObjectPool ObjectPool;
        public EventSystem EventSystem;
        public TableComponent TableComponent;
        public FrameComponent FrameComponent { get; private set; }
    }
}